<template>
  <view class="page-login box pd-v-60 flex flex-ver flex-center position-r">
    <image class="" src="@/static/avatar.png" style="width:152rpx" mode="widthFix" />

    <view class="mr-t-24 font44 lh-60 bold color-default">
        鸽充充
    </view>

    <view class="mr-t-14 font30 lh-42 color-info">
        行驶保驾护航，充电轻松安心
    </view>
    
    <view class="full-w box mr-t-100 pd-t-90">
      <u-button class="mr-t-24" type="primary" :customStyle="{height:'88rpx'}" shape="circle" color="#35BE65" open-type="getPhoneNumber" @getphonenumber="getphonenumber"  text="一键登录"></u-button>
      <!-- <u-button class="mr-t-24" type="primary" :customStyle="{height:'88rpx'}" shape="circle" color="#35BE65" @click="loginFn"  text="一键登录"></u-button> -->
    </view>

    <view class="mr-t-56 flex flex-center">
        <view class="mr-r-12 pd-h-10" @click="checkChange">
            <radio :checked="isCheck" style="transform:scale(0.8);"></radio>
            <!-- <u-icon :name="isCheck ? 'checkmark-circle-fill' : 'checkmark-circle'" :color="isCheck ? '#3AC981' : '#666'" size="20" ></u-icon> -->
        </view>
          
        <view class="font24 lh-34 color-default">
            请您仔细阅读并充分理解相关条款，点击同意即代表您已阅读并同意 <text class="font24 lh-34 color-main">《用户协议》</text>和<text class="font24 lh-34 color-main">《隐私政策》</text>
        </view>
          
    </view>

    <view class="page-login__footer flex flex-j-center flex-center full-w position-a">
        <view class="color-note font28 lh-40" @click="back">
            暂不登录
        </view>
          
    </view>
      
      
      
   
      
      
  </view>
</template>

<script setup>
import { useStore } from '@/store/index.js';
import { login } from "@/api/login";

import { computed, ref, onMounted, reactive } from "vue";
import {getPhone} from '@/api/user'

const store = useStore();

const isWx = ref(false)
const redirectURL = ref('/pages/index/index')
const isCheck = ref(false)
const code = ref('')

onMounted(() => {
    
});

const back = () => {
    uni.navigateBack({
        delta: 1
    });
}


const loginFn = () => {
  if(!isCheck.value) {
    uni.showToast({
      title: '请先阅读并同意相关条款',
      icon: 'none'
    })
    return
  }
  uni.showLoading({ title: "正在加载...", mask: true });
  uni.login({
      provider: 'weixin',
      success: (res) => {
        code.value = res.code
        
        login({
          "code": code.value,
        }).then(result => {
          uni.hideLoading()
          if(result.code == 200) {
            result.data.nikeName = 'nikeName'
            uni.setStorageSync('token', result.data.token)
            uni.setStorageSync('userInfo', result.data)
            store.user.SET_USERINFO(result.data)
            store.user.SET_TOKEN(result.data.token)
            store.common.SET_TABINDEX(0);
            uni.reLaunch({
                url: '/pages/index/index'
            })
            // if(result.data.nikeName && result.data.userAvatar) {
            //     uni.setStorageSync('token', result.data.token)
            //     uni.setStorageSync('userInfo', result.data)
            //     store.user.SET_USERINFO(result.data)
            //     store.user.SET_TOKEN(result.data.token)
            //     store.common.SET_TABINDEX(0);
            //     uni.reLaunch({
            //         url: '/pages/index/index'
            //     })
            // } else {
            //     uni.setStorageSync('info', result.data)
            //     uni.navigateTo({
            //         url: '/sub_user/pages/informationAdd/informationAdd'
            //     })
            // }
          }else {
                              uni.showToast({
                                  title: res.msg,
                                  icon: 'none'
                              })
                          }
          console.log(result)
        }).catch(err => {
            console.log(err,'errrrrrr')
          uni.hideLoading()
        })
      }
  })
}

const getphonenumber = (resPhone) => {
    console.log(resPhone,'resPhone')
    
    if(!resPhone.detail.code) {
        return 
    }
    if(isCheck.value) {
        uni.showLoading({ title: "正在加载...", mask: true });
        getPhone({
            // "phoneCode": code.value,
            "phoneCode": resPhone.detail.code,
            // "encryptedData": resPhone.detail.encryptedData,
            // "iv": res.detail.iv,
        }).then(result => {
            if(result.code == 200) {
                let phone = result.data

                uni.login({
                    provider: 'weixin',
                    success: (res) => {
                        code.value = res.code,
                        
                        login({
                            code: code.value,
                            phoneNumber:phone||''
                        }).then(result => {
                        uni.hideLoading()
                        if(result.code == 200) {

                            if(result.data.nikeName && result.data.userAvatar) {
                                
                                uni.setStorageSync('token', result.data.token)
                                uni.setStorageSync('userInfo', result.data)
                                store.user.SET_USERINFO(result.data)
                                store.user.SET_TOKEN(result.data.token)
                                store.common.SET_TABINDEX(0);
                                uni.reLaunch({
                                    url: '/pages/index/index'
                                })
                            } else {
                                uni.setStorageSync('info', result.data)
                                uni.navigateTo({
                                    url: '/sub_user/pages/informationAdd/informationAdd'
                                })
                            }
                            
                            // uni.reLaunch({
                            //     url: '/pages/index/index'
                            // })
                            // if(result.data.nikeName && result.data.userAvatar) {
                            //     uni.setStorageSync('token', result.data.token)
                            //     uni.setStorageSync('userInfo', result.data)
                            //     store.user.SET_USERINFO(result.data)
                            //     store.user.SET_TOKEN(result.data.token)
                            //     store.common.SET_TABINDEX(0);
                            //     uni.reLaunch({
                            //         url: '/pages/index/index'
                            //     })
                            // } else {
                            //     uni.setStorageSync('info', result.data)
                            //     uni.navigateTo({
                            //         url: '/sub_user/pages/informationAdd/informationAdd'
                            //     })
                            // }
                        } else {
                              uni.showToast({
                                  title: res.msg,
                                  icon: 'none'
                              })
                          }
                        console.log(result)
                        }).catch(err => {
                            console.log(err,'errrrrrr')
                        uni.hideLoading()
                        })
                    }
                })
            }else {
                              uni.showToast({
                                  title: res.msg,
                                  icon: 'none'
                              })
                          }
        })
    } else {
        uni.showToast({
            title: '请先阅读并同意相关条款',
            icon: 'none'
        })
    }
  
  
//   console.log(resPhone);
//   getPhone({
//     // "phoneCode": code.value,
//     "phoneCode": resPhone.detail.code,
//     // "encryptedData": resPhone.detail.encryptedData,
//     // "iv": res.detail.iv,
//   }).then(result => {
//     console.log(result)
//   })
    
}

const checkChange = () => {
    isCheck.value = !isCheck.value
}

</script>

<style lang="scss" scoped>
.page-login {
    height:100vh;
    padding-top:276rpx;

    .page-login__footer  {
        left:0;
        bottom:68rpx;
    }
}
</style>
